package com.lcu.wl.modules.tools.controller;


import com.lcu.wl.annotation.Log;
import com.lcu.wl.common.utils.PageUtils;
import com.lcu.wl.common.utils.SYSDATA;
import com.lcu.wl.common.validator.ValidatorUtils;
import com.lcu.wl.modules.tools.entity.QuartzJob;
import com.lcu.wl.modules.tools.service.IQuartzJobService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 定时任务 前端控制器
 * </p>
 *
 * @author wanglei
 * @since 2021-03-15
 */
@RestController
@RequestMapping("/tools/quartz-job")
public class QuartzJobController {

    @Autowired
    private IQuartzJobService iQuartzJobService;

    @GetMapping("/list")
    @ApiOperation("定时任务查询")
    @RequiresPermissions("tools:quartz:list")
    public SYSDATA list(@RequestParam Map<String,Object> params){
        PageUtils page = iQuartzJobService.queryPage(params);
        return SYSDATA.ok().put("page",page);
    }

    /**
     * 导出定时任务
     * @param params
     * @param response
     * @throws IOException
     */
    @Log("导出定时任务数据")
    @ApiOperation("定时任务导出")
    @GetMapping("/download")
    @RequiresPermissions("tools:quartz:export")
    public void download(@RequestParam Map<String,Object> params, HttpServletResponse response) throws IOException {
        List<QuartzJob> quartzJobs = iQuartzJobService.queryAll(params);
        iQuartzJobService.download(quartzJobs,response);
    }

    /**
     * 定时任务信息
     */
    @GetMapping("/info/{jobId}")
    @ApiOperation("根据jobId获取定时任务信息")
    @RequiresPermissions("tools:quartz:info")
    public SYSDATA info(@PathVariable("jobId") Long jobId){
        QuartzJob quartzJob = iQuartzJobService.getById(jobId);
        return SYSDATA.ok().put("quartzJob", quartzJob);
    }

    /**
     * 保存定时任务
     */
    @Log("保存定时任务")
    @ApiOperation("新增定时任务")
    @PostMapping("/save")
    @RequiresPermissions("tools:quartz:save")
    public SYSDATA save(@RequestBody QuartzJob quartzJob){
        ValidatorUtils.validateEntity(quartzJob);
        iQuartzJobService.saveJob(quartzJob);
        return SYSDATA.ok();
    }

    /**
     * 修改定时任务
     */
    @Log("修改定时任务")
    @ApiOperation("修改定时任务")
    @PostMapping("/update")
    @RequiresPermissions("tools:quartz:update")
    public SYSDATA update(@RequestBody QuartzJob quartzJob){
        ValidatorUtils.validateEntity(quartzJob);
        iQuartzJobService.update(quartzJob);
        return SYSDATA.ok();
    }

    /**
     * 删除定时任务
     */
    @Log("删除定时任务")
    @ApiOperation("删除定时任务")
    @PostMapping("/delete")
    @RequiresPermissions("tools:quartz:delete")
    public SYSDATA delete(@RequestBody Long[] jobIds){
        iQuartzJobService.deleteBatch(jobIds);

        return SYSDATA.ok();
    }

    /**
     * 立即执行任务
     */
    @Log("执行定时任务")
    @ApiOperation("立即执行定时任务")
    @PostMapping("/run")
    @RequiresPermissions("tools:quartz:run")
    public SYSDATA run(@RequestBody Long jobId){
        iQuartzJobService.run(jobId);
        return SYSDATA.ok();
    }

    /**
     * 暂停定时任务
     */
    @Log("暂停定时任务")
    @ApiOperation("暂停定时任务")
    @PostMapping("/pause")
    @RequiresPermissions("tools:quartz:pause")
    public SYSDATA pause(@RequestBody Long jobId){
        iQuartzJobService.pause(jobId);
        return SYSDATA.ok();
    }

    /**
     * 恢复定时任务
     */
    @Log("恢复定时任务")
    @ApiOperation("恢复定时任务")
    @PostMapping("/resume")
    @RequiresPermissions("tools:quartz:resume")
    public SYSDATA resume(@RequestBody Long jobId){
        iQuartzJobService.resume(jobId);
        return SYSDATA.ok();
    }

}
